package com.ctrip.ec.biz.dao.dict;
import com.ctrip.ec.biz.entity.dict.DictCategory;
import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.dict.DictEntity;
import com.ctrip.ec.dal.DbAccess;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class DictDao extends DaoBase<DictEntity> {
    public DictDao() {
        super(DictEntity.class);
    }
    public DictDao(DbAccess db) {
        super(DictEntity.class,db);
    }

    public List<DictEntity> list(DictCategory category) throws SQLException {

        String sql = "select * from " + this.getTableName() + " where category=?";
        List<DictEntity> list = this.db.query(DictEntity.class,sql,category.getCode());
        return list;
    }

    public DictEntity findByCode(String category, String code) throws SQLException {
        String sql = "select * from " + this.getTableName() + " where category=@0 and code=@1";
        //sql = "select * from " + this.getTableName() + " where category='" + category + "' and code='" + code + "'";
        List<DictEntity> list =  this.db.query(DictEntity.class,sql,category,code);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
